﻿#include "VAOneXMLAdaptorAcousticsNCTPerforated.h"

#include "FITKAcousticsPhysics/FITKAcousticsNCTPerforated.h"

namespace IO
{

    bool VAOneXMLAdaptorAcousticsNCTPerforated::adaptR()
    {
        return readNCTPerforated();
    }

    bool VAOneXMLAdaptorAcousticsNCTPerforated::adaptW()
    {
        return writeNCTPerforated();
    }

    bool VAOneXMLAdaptorAcousticsNCTPerforated::readNCTPerforated()
    {
        if (!_node)// 判断节点是否存在
        {
            return false;
        }

        bool b = true;
        //转换类型
        Acoustics::FITKAcousticsNCTPerforated* layerData = dynamic_cast<Acoustics::FITKAcousticsNCTPerforated*>(_dataObj);

        if(!layerData)
        {
            return false;
        }

        double thickness = readDoubleAttribute(_node, "thickness", &b);

        if(b)
        {
            layerData->setThickness(thickness);
        }

        double density = readDoubleAttribute(_node, "density", &b);

        if(b)
        {
            layerData->setDensity(density);
        }

        double flowResistivity = readDoubleAttribute(_node, "flow_resistivity", &b);

        if(b)
        {
           layerData->setFlowResistivity(flowResistivity);
        }

        double areaRatio = readDoubleAttribute(_node, "area_ratio", &b);

        if(b)
        {
            layerData->setAreaRatio(areaRatio);
        }

        double radius = readDoubleAttribute(_node, "radius", &b);

        if(b)
        {
            layerData->setRadius(radius);
        }

        double porosity = readDoubleAttribute(_node, "porosity", &b);

        if(b)
        {
            layerData->setPorosity(porosity);
        }

        QString fluidMaterial = readStrAttribute(_node, "fluid", &b);
        if(b)
        {
            int fluidMaterialID = getMaterialObjectIdByName(fluidMaterial);
            layerData->setfluidMaterialID(fluidMaterialID);
        }
        else
        {
            //读材料读取异常
            consoleMessage(3,"Read FluidMaterial Error" + fluidMaterial);
        }

        int solidType = readIntAttribute(_node, "solidType", &b);
        if(b)
        {
            layerData->setSolidType(static_cast<Acoustics::FITKAcousticsNCTPerforated::NCTSolidType>(solidType));
        }

        int perforationType = readIntAttribute(_node, "perforationType", &b);
        if(b)
        {
            layerData->setPerforationType(static_cast<Acoustics::FITKAcousticsNCTPerforated::PerforationType>(perforationType));
        }

        QString material = readStrAttribute(_node, "material", &b);

        if(b)
        {
            int materialID = getMaterialObjectIdByName(material);
            layerData->setMaterialID(materialID);
        }
        else
        {
            //读材料读取异常
            consoleMessage(3,"Read Material Error" + material);
        }

        return true;
    }

    bool VAOneXMLAdaptorAcousticsNCTPerforated::writeNCTPerforated()
    {
        if (!_node)// 判断节点是否存在
        {
            return false;
        }

        //转换类型
        Acoustics::FITKAcousticsNCTPerforated* data = dynamic_cast<Acoustics::FITKAcousticsNCTPerforated*>(_dataObj);

        if(!data)
        {
            return false;
        }

        //创建常规板节点
        rapidxml::xml_node<char>* node = createNodeElement(_node, "PerforatedTrimLayer");

        if(!node)
        {
            return false;
        }

        //写如属性值
        //写厚度
        writeAttribute(node, "density", data->getDensity());
        writeAttribute(node, "thickness", data->getThickness());
        writeAttribute(node, "flow_resistivity", data->getFlowResistivity());
        writeAttribute(node, "area_ratio", data->getAreaRatio());
        writeAttribute(node, "radius", data->getRadius());
        writeAttribute(node, "porosity", data->getPorosity());
        writeAttribute(node, "material", getMaterialObjectNameById(data->getMaterialID()));
        writeAttribute(node, "fluid", getMaterialObjectNameById(data->getfluidMaterialID()));
        Acoustics::FITKAcousticsNCTPerforated::NCTSolidType solidType = data->getSolidType();
        writeAttribute(node, "solidType", solidType);
        writeAttribute(node, "perforationType", data->getPerforationType());
        return true;
    }

}
